<?php

namespace app\admin\controller;
use app\admin\model\LogModel;
use app\admin\model\CepingLogModel as OrderModel;
use think\Db;
use com\IpLocation;
use think\Request;

class Order extends Base
{

    /**
     * [operate_log 操作日志]
     * @return [type] [description]
     * @author felix
     */
    public function index()
    {

        
        $realname = input('realname');
        $phone = input('phone');
        $add_time = input('add_time');
        $map = [];

        if($realname&&$realname!==""){
            $map['think_member.realname'] = ['like',"%" . $realname . "%"];
        }

        if($phone&&$phone!==""){
            $map['think_member.phone'] = ['like',"%" . $phone . "%"];
        }


        if($add_time&&$add_time!=="")
        {
            $tt = explode('~',$add_time);
            $map['think_member.create_time'] = ['between',[strtotime(trim($tt[0])),strtotime(trim($tt[1]).' 23:59:59')]];
        }
       // dump($map);
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('ceping_log')->alias('p')->join('think_member','p.uid = think_member.id','LEFT')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $data = new OrderModel();
        $lists = $data->getDataByWhere($map, $Nowpage, $limits);
        if($lists){
            foreach ($lists as $k => $v){
                
            }
        }
        //$lists = [];
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('count', $count);

        $this->assign('realname', $realname);
        $this->assign('add_time', $add_time);
        $this->assign('phone', $phone);

        $no_ajax = input('no_ajax') ? true : false;
        $this->assign('page', $Nowpage);

        if(input('get.page') && !$no_ajax ){
            return json($lists);
        }
        return $this->fetch();
    }



    public function detail()
    {
        $id = input('id');
        $page = input('page');
       
        $order = db('ceping_log')->where('id',$id)->find();
        $answers = json_decode($order['answers'],true);
      

        $question_list = db('ceping_question')->where('cp_id',$order['cp_id'])->where('is_show',1)->order('sort asc')->select();
        foreach ($question_list as $k => $v)
        {
            $options_arr = json_decode($v['options'],true);
			$options = array_column($options_arr,'title');
			$question_list[$k]['answer'] = !empty($options[$answers[$v['id']]])?$options[$answers[$v['id']]]:'';

        }


        $this->assign('question_list', $question_list);
        $this->assign('order', $order);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function detail111111()
    {
        $id = input('id');
        $page = input('page');
        $doctor = [];
        $order = db('order')->where('id',$id)->find();
        //-1 未支付 1已支付未匹配陪诊师 2已匹配服务中 3 完成  4 客户已取消  5 客户申请退款中  6 退款完成
        $status = ['-1'=>'待支付','1'=>'已支付'];
        $arr = ['member'=>[],'doctor'=>[],'reservation'=>[],'address'=>[],'order_goods'=>[],'log'=>[],'order'=>[],'ship_log'=>[]];
        $result_id = Db::name('order_question_result')->where('id',$order['result_id'])->value('result_id');
        $rr = Db::name('option_result')->where('id',$result_id)->field('id,name,result_remark,job_desc')->find();;
        $arr['order']['name'] = $rr['name'];
        $arr['order']['add_time'] = date('Y-m-d H:i:s',$order['add_time']);


        $goods = db('question_log')->where('question_no',$order['question_no'])->select();
        $ppp = db('order_question_result')->where('question_no',$order['question_no'])->value('point_desc');
        $ppp = json_decode($ppp,true);
        foreach ($goods as $k => $v)
        {
            $gg = Db::name('question_option')->where('id',$v['option_id'])->find();
            $c_name = db('question_list')->where('id',$gg['question_list_id'])->value('title');
            $arr['order_goods'][$k]['id'] = $v['id'];
            $arr['order_goods'][$k]['q_name'] = $c_name;
            $arr['order_goods'][$k]['o_name'] = $gg['title'];
            $arr['order_goods'][$k]['age'] = $ppp[$v['option_id']]??49;

        }


        $this->assign('order', $arr);
        $this->assign('id', $id);
        $this->assign('page', $page);

        return $this->fetch();
    }


}